package com.edu.Controller;

import com.edu.entity.User;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;

@WebServlet(name = "RegisetServlet", value = "/RegisetServlet")
public class RegisetServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        String username=request.getParameter("username");
        String userpass=request.getParameter("userpass");
        String name=request.getParameter("name");
        if (username.equals("")||userpass.equals("")){
            request.setAttribute("meg","账号或者密码不能为空");
            request.getRequestDispatcher("regiset.jsp").forward(request, response);
        }
        UserService us=new UserServiceImpl();
        boolean x=us.selectUser(username);
        if(x){
            request.setAttribute("meg","该账号已拥有");
            request.getRequestDispatcher("regiset.jsp").forward(request, response);
        }else{
            boolean isAdd=us.addUser(username,userpass,name);
            if (isAdd){
                request.setAttribute("meg",name);
                request.getRequestDispatcher("book.jsp").forward(request, response);
            }else {
                request.setAttribute("meg","出错了注册失败重新注册");
                request.getRequestDispatcher("regiset.jsp").forward(request, response);
            }
        }
    }
}

